package pl.mcnova.view.panels;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.table.TableRowSorter;

import pl.mcnova.model.dao.DeviceDAO;
import pl.mcnova.model.entity.Device;
import pl.mcnova.model.table.DevicesTableModel;

public class DevicesMainPanel extends JPanel {

	private DevicesTableModel model;
	private JScrollPane scrollPane;
	private JTable table;
	private DeviceDAO deviceDAO;
	private TableRowSorter<DevicesTableModel> sorter;

	public DevicesMainPanel(String name) {
		setLayout(new BorderLayout());
		setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(name),
				BorderFactory.createEmptyBorder(0, 6, 6, 6)));

		deviceDAO = new DeviceDAO();

		model = new DevicesTableModel(deviceDAO.getAllGroupedByNumbers("producer"));
		table = new JTable(model);
		table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

		scrollPane = new JScrollPane(table);

		sorter = new TableRowSorter<DevicesTableModel>(model);
		table.setRowSorter(sorter);

		add(scrollPane, BorderLayout.CENTER);
	}

	public ListSelectionModel getTable() {
		return table.getSelectionModel();
	}

	public int getSelected() {
		int[] selected = table.getSelectedRows();

		if (selected == null || selected.length == 0)
			return -1;
		else {
			if (sorter != null) return sorter.convertRowIndexToModel(selected[0]);
			return selected[0];
		}

	}

	public void runFilter(String producer, String model) {

		List<RowFilter<Object, Object>> filters = new ArrayList<RowFilter<Object, Object>>(2);
		filters.add(RowFilter.regexFilter(producer, 1));
		filters.add(RowFilter.regexFilter(model, 2));

		RowFilter<Object, Object> rf = RowFilter.andFilter(filters);
		sorter.setRowFilter(rf);
	}

	public int[] getAllSelected() {
		int[] selected = table.getSelectedRows();

		if (selected == null || selected.length == 0)
			return null;
		else {
			if (sorter != null) for (int i = 0; i < selected.length; i++)
				selected[i] = sorter.convertRowIndexToModel(selected[i]);

			return selected;
		}
	}

	public void clearSelection() {
		table.getSelectionModel().removeSelectionInterval(table.getSelectionModel().getMinSelectionIndex(),
				table.getSelectionModel().getMaxSelectionIndex());
	}

	public void add(Device d) {
		model.add(d);
	}

	public Device getDevice(int index) {
		return model.get(index);
	}

	public void remove(Device d) {
		model.remove(d);
	}

	public void runFilter(Device d) {

		List<RowFilter<Object, Object>> filters = new ArrayList<RowFilter<Object, Object>>();

		if (d == null) {
			filters.add(RowFilter.regexFilter("", 1));
			filters.add(RowFilter.regexFilter("", 2));

		} else {
			filters.add(RowFilter.regexFilter("(?i)" + d.getProducer(), 1));
			filters.add(RowFilter.regexFilter("(?i)" + d.getModel(), 2));

		}

		RowFilter<Object, Object> rf = RowFilter.andFilter(filters);
		sorter.setRowFilter(rf);
	}

	public void runFilter() {

		List<RowFilter<Object, Object>> filters = new ArrayList<RowFilter<Object, Object>>();

		filters.add(RowFilter.regexFilter("", 1));
		filters.add(RowFilter.regexFilter("", 2));

		RowFilter<Object, Object> rf = RowFilter.andFilter(filters);
		sorter.setRowFilter(rf);
	}

}
